//
//  messagedb.m
//  tcpoctest
//
//  Created by apple on 13-3-18.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "messagedb.h"
#import "XMMessageInfo.h"
#import "MessageDataSource.h"

@implementation messagedb

-(void)creatTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    [db executeUpdate:@"create table if not exists message(mail text,\
firendmail text,messagetype int,isprocess int,mindex int,msgid text,msgtime text)"];
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)updateTable
{
    if (db == nil) {
        return;
    }
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    
    if ([db columnExists:@"msgbadge" inTableWithName:@"message"] == NO) {
        NSString *strsql = @"ALTER TABLE message ADD msgbadge int";
        
        [db executeUpdate:strsql];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)initData
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"SELECT \
firendmail,messagetype,isprocess,msgid,msgtime,msgbadge FROM message where mail = '%@'",
              [DataControl shareControl].personDS.uinfo.mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    FMResultSet *s = [db executeQuery:strsql];
    
    [[DataControl shareControl].messageDS DelAllMsgArr];
    [DataControl shareControl].mBadgeCount = 0;
    
    while ([s next])
    {
        XMMessageInfo *xmminfo = [[XMMessageInfo alloc] init];
        xmminfo.strmail = [s stringForColumn:@"firendmail"];
        xmminfo.messageType = [s intForColumn:@"messagetype"];
        xmminfo.mbAlreadayArgee = [s intForColumn:@"isprocess"];
        xmminfo.mindex = [[DataControl shareControl].messageDS GetMagArrCount];
        xmminfo.strmsgid = [s stringForColumn:@"msgid"];
        xmminfo.strtime = [s stringForColumn:@"msgtime"];
        xmminfo.badgeNum = [s intForColumn:@"msgbadge"];
        
        if (xmminfo.mbAlreadayArgee == false)
        {
            [DataControl shareControl].mBadgeCount += xmminfo.badgeNum;
        }
        
        [[DataControl shareControl].messageDS addMsg:xmminfo];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
    
    [[DataControl shareControl].UICommendDS addUICommand:UC_SETBADGE];
}

-(void)insertMessage:(XMMessageInfo *)mi
{
    if (db == nil)
    {
        return;
    }
    
    NSString    *strsql = [NSString stringWithFormat:@"insert into message \
(mail,firendmail,messagetype,isprocess,mindex,msgid,msgtime,msgbadge) \
values('%@','%@',%d,%d,%d,'%@','%@',%d)"
                           ,[DataControl shareControl].personDS.uinfo.mail
                           ,mi.strmail
                           ,mi.messageType
                           ,mi.mbAlreadayArgee
                           ,mi.mindex
                           ,mi.strmsgid
                           ,mi.strtime
                           ,mi.badgeNum];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)deleteMessage:(NSString *)mail
{
    if (db == nil) {
        return;
    }
    
    NSString *strsql = [NSString stringWithFormat:@"DELETE FROM message WHERE mail='%@' AND firendmail = '%@'",
                        [DataControl shareControl].personDS.uinfo.mail
                        ,mail];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(void)updateMessage:(XMMessageInfo *)mi
{
    if (db == nil) {
        return;
    }
    
    NSString    *strsql = [NSString stringWithFormat:@"update message \
set isprocess = %d ,msgtime = '%@',msgbadge = %d \
where mail = '%@' and  firendmail = '%@' and messagetype = %d \
and msgid = '%@'"
                           , mi.mbAlreadayArgee
                           ,mi.strtime
                           ,mi.badgeNum
                           ,[DataControl shareControl].personDS.uinfo.mail
                           ,mi.strmail
                           ,mi.messageType
                           ,mi.strmsgid];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    [db executeUpdate:strsql];
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
}

-(NSString *)getMessageID:(XMMessageInfo *)im
{
    NSString *strsql = [NSString stringWithFormat:@"SELECT msgid FROM message \
where mail = '%@' AND firendmail = '%@' and messagetype = %d",
                        [DataControl shareControl].personDS.uinfo.mail
                        ,im.strmail
                        ,im.messageType];
    
    pthread_mutex_lock(&mutexdatabase);
    
    [db open];
    FMResultSet *s = [db executeQuery:strsql];
    
    NSString *strindex = @"";
    if([s next])
    {
        strindex = [s stringForColumn:@"msgid"];
    }
    
    [db close];
    
    pthread_mutex_unlock(&mutexdatabase);
    
    return strindex;
}

@end
